#ifndef LONGKEY_BREAKPAD_PROCESSOR_SOURCE_LINE_RESOLVER_BASE_H_
#define LONGKEY_BREAKPAD_PROCESSOR_SOURCE_LINE_RESOLVER_BASE_H_

#include <map>
#include <string>
#include "source_line_resolver_interface.h"

namespace longkey_breakpad
{
	using std::map;

	// Forward declaration.
	// ModuleFactory is a simple factory interface for creating a Module instance
	// at run-time.
	class ModuleFactory;

	class SourceLineResolverBase : public SourceLineResolverInterface
	{
	public:
		// Read the symbol_data from a file with given file_name.
		// The part of code was originally in BasicSourceLineResolver::Module's
		// LoadMap() method.
		// Place dynamically allocated heap buffer in symbol_data. Caller has the
		// ownership of the buffer, and should call delete [] to free the buffer.
		static bool ReadSymbolFile(char **symbol_data, const string &file_name);

	protected:
		// Users are not allowed create SourceLineResolverBase instance directly.
		SourceLineResolverBase(ModuleFactory *module_factory);
		virtual ~SourceLineResolverBase();

		// Virtual methods inherited from SourceLineResolverInterface.
		virtual bool LoadModule(const CodeModule *module, const string &map_file);
		virtual bool LoadModuleUsingMapBuffer(const CodeModule *module,
			const string &map_buffer);
		virtual bool LoadModuleUsingMemoryBuffer(const CodeModule *module,
			char *memory_buffer);
		virtual bool ShouldDeleteMemoryBufferAfterLoadModule();
		virtual void UnloadModule(const CodeModule *module);
		virtual bool HasModule(const CodeModule *module);
		virtual void FillSourceLineInfo(StackFrame *frame);
		virtual WindowsFrameInfo *FindWindowsFrameInfo(const StackFrame *frame);
		virtual CFIFrameInfo *FindCFIFrameInfo(const StackFrame *frame);

		// Nested structs and classes.
		struct Line;
		struct Function;
		struct PublicSymbol;
		struct CompareString {
			bool operator()(const string &s1, const string &s2) const;
		};
		// Module is an interface for an in-memory symbol file.
		class Module;
		class AutoFileCloser;

		// All of the modules that are loaded.
		typedef map<string, Module*, CompareString> ModuleMap;
		ModuleMap *modules_;

		// All of heap-allocated buffers that are owned locally by resolver.
		typedef std::map<string, char*, CompareString> MemoryMap;
		MemoryMap *memory_buffers_;

		// Creates a concrete module at run-time.
		ModuleFactory *module_factory_;

	private:
		// ModuleFactory needs to have access to protected type Module.
		friend class ModuleFactory;

		// Disallow unwanted copy ctor and assignment operator
		SourceLineResolverBase(const SourceLineResolverBase&);
		void operator=(const SourceLineResolverBase&);
	};
}


#endif	// LONGKEY_BREAKPAD_PROCESSOR_SOURCE_LINE_RESOLVER_BASE_H_